package com.tang.managementdemo.util;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class RecordTimeAspect {
    @Around("execution(* com.tang.managementdemo.service.*.*(..))")
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
        //记录方法的执行时间
        long start = System.currentTimeMillis();

        //记录原始方法
        Object result = joinPoint.proceed();

        //记录方法执行结束时间
        long end = System.currentTimeMillis();

        //记录方法执行时间
        log.info("{} cost {} ms", joinPoint.getSignature(), end - start);
        return result;
    }
}
